//
//  Tools.swift
//  NetProject
//
//  Created by EDY on 2021/11/8.
//

import Foundation
import UIKit

class Tools: NSObject {
    
    fileprivate let alertTag = 789987
    fileprivate var alertView : AlertView?
    
    // 弹出确认+取消按钮的通用弹框
    func alert(_ message:String,_ confirmBlock:@escaping ( ()->Void), _ cancelBlock:@escaping (() -> Void)){
//        alertView?.removeFromSuperview()
        let config = ShowAlertConfig()
        alertView = AlertView.init(title: message, attributedTitle: nil, titleImage: nil, message: "", attributedMessage: nil, leftBtnTitle: "确定", leftBtnAttributedTitle: nil, rightBtnTitle: "取消", rightBtnAttributedTitle: nil, config: config)
        alertView?.leftBlock = confirmBlock
        alertView?.rightBlock = cancelBlock
        if let window = UIApplication.shared.currentWindow {
            if let vc = window.rootViewController{
                if let currentVc = UIViewController.current(base: vc){
                    currentVc.view.addSubview(alertView!)
                }
                
            }else{
                window.addSubview(alertView!)
            }
            
            alertView?.snp.remakeConstraints({ make in
                make.centerX.equalToSuperview()
                make.centerY.equalToSuperview()
                make.width.equalTo(config.width)
            })
            
        }
    }
    
    // 弹出全屏遮罩层 + view
    func popView(_ contentView:UIView, _ size:CGSize, _ hideHandler:@escaping (()->Void)){
        let config = ShowPopViewConfig()
        let popView = PopView(contentView: contentView, config: config,size: size, hidenHandle: hideHandler)
        if let window = UIApplication.shared.currentWindow {
            window.addSubview(popView)
        }
    }
    
    //弹出actionsheet
    func alertActionSheet(_ title:String,_ contents:[String] , _ needCancelItem:Bool, _ clickBlock:((_ index:Int)->Void)?){
        let alert = UIAlertController(title: title, message: "", preferredStyle: .actionSheet)
        if needCancelItem{
            let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { action in
                if let clickBlock = clickBlock {
                    clickBlock(-1)
                }
            }
            alert.addAction(cancelAction)
        }
        for i in 0..<contents.count{
            let action = UIAlertAction(title: contents[i], style: .default) { _ in
                if let clickBlock = clickBlock {
                    clickBlock(i)
                }
            }
            alert.addAction(action)
        }
        if let vc = UIApplication.shared.currentWindow?.rootViewController{
            let myVC = UIViewController.current(base: vc)!
            myVC.present(alert, animated: true, completion: nil)
        }
    }
    
    fileprivate var hud : MBProgressHUD? = nil
    // 显示加载框
    func showLoading(msg:String) -> Void {
        self.hideLoading()
        DispatchQueue.main.async {
            if (self.hud == nil){
                self.hud = MBProgressHUD.init(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: UIScreen.main.bounds.size.height))
                self.hud?.tag = 98767
            }
            self.hud?.backgroundView.style = .solidColor
            UIApplication.shared.currentWindow?.addSubview(self.hud!)
            self.hud?.label.text = msg
            self.hud?.show(animated: true)
        }
    }
    
    func  hideLoading() -> Void {
        DispatchQueue.main.async {
            if self.hud == nil{
                self.hud = UIApplication.shared.currentWindow?.viewWithTag(98767) as? MBProgressHUD
            }
            self.hud?.hide(animated: true)
            self.hud?.removeFromSuperview()
        }
    }
    
    class var shared: Tools {
        var instance = Tools()
        DispatchQueue.once {
            instance = Tools.init()
        }
        return instance
    }
    
    private override init() {
        
    }
}
